﻿using ConfExpert.Forms.Aksesuar;
using ConfExpert.Forms.Base;
using ConfExpert.Properties;
using ConfExpert.Utility;
using DevExpress.Utils;
using DevExpress.XtraGrid.Views.Grid;
using DevTeam;
using System;
using System.Data;
using System.Drawing;
using System.Windows.Forms;

namespace ConfExpert.Forms.Depo.Aksesuar
{
    public partial class AksesuarIstekler : XForm
    {
        public AksesuarIstekler()
        {
            InitializeComponent();
        }

        public AksesuarIstekler(bool isOnay)
            : this()
        {
            IsOnay = isOnay;
        }

        BindingSource bs1, bs3;
        bool IsOnay = false;
        private void Istekler_Load(object sender, EventArgs e)
        {

            this.Text = IsOnay ? "Onayımı Bekleyen Aksesuar İstekleri" : "Aksesuar İsteklerim";

            bs1 = gvIstek.Init("GetAksesuarIstek", IsOnay, WhoAmI);
            bs1.CurrentItemChanged += new EventHandler(bs1_CurrentItemChanged);

            //bs2 = gvIstek.AddDetailGrid(gvAksesuar, 1,"AksesuarIstekId");
            bs3 = gvIstek.AddDetailGrid(gvAyrim, "AksesuarIstekId");

            //gvAksesuar.SutunEkle("Qty", typeof(decimal), "SUM(Child({FK}).QtySelected)");

            gvAyrim.AddColumn("O", typeof(int), "IIF(Onay=1, 1, 0)");
            gvAyrim.AddColumn("R", typeof(int), "IIF(Onay=0, 1, 0)");
            gvAyrim.AddColumn("I", typeof(int), "IIF(Onay is null, 1, 0)");
            //gvAksesuar.SutunEkle("O", typeof(int), "SUM(Child({FK}).O)");
            //gvAksesuar.SutunEkle("R", typeof(int), "SUM(Child({FK}).R)");
            //gvAksesuar.SutunEkle("I", typeof(int), "SUM(Child({FK}).I)");
            gvIstek.AddColumn("O", typeof(int), "SUM(Child({FK}).O)");
            gvIstek.AddColumn("R", typeof(int), "SUM(Child({FK}).R)");
            gvIstek.AddColumn("I", typeof(int), "SUM(Child({FK}).I)");

            gvIstek.SetCaptions("BrandId:Nereye?");

            gvIstek.Columns["O"].ToolTip = "Onay";
            gvIstek.Columns["R"].ToolTip = "Red";
            gvIstek.Columns["I"].ToolTip = "İstek";
            gvAyrim.MakeColumnInvisible("O", "R", "I", "AyrimId");
            gvIstek.FormatAsNumber(0, "O", "R", "I");
            //gvAksesuar.FormatAsNumber(0, "O", "R", "I");

            if (IsOnay)
            {
                GridManager gm;
                gm = GridManager.GetManager(gvAyrim);
                gm.GridMenu.AddMenu("Onay", TopuSec, Resources.accept_16);
                gm.GridMenu.AddMenu("Red", TopuBirak, Resources.delete_16);

                //gm = GridManager.GetManager(gvAksesuar);
                //gm.GridMenu.AddMenu("Onay", ArtikeliSec, Resources.accept_16);
                //gm.GridMenu.AddMenu("Red", ArtikeliBirak, Resources.delete_16);

                gm = GridManager.GetManager(gvIstek);
                gm.GridMenu.AddMenu("Onay", IstegiSec, Resources.accept_16);
                gm.GridMenu.AddMenu("Red", IstegiBirak, Resources.delete_16);

                if (ytk(148))
                    gvIstek.MakeReadOnly("SezonId", "OrderRef");
                else
                    gvIstek.MakeReadOnly();
                bbiKaydet.Visibility = DevExpress.XtraBars.BarItemVisibility.Always;
            }
            else
                gvIstek.MakeReadOnly();

            gvAyrim.BestFitColumns();

            splitContainerControl.AutoSize();

            //gvAksesuar.MakeReadOnly();
            gvAyrim.MakeReadOnly("QtyRevised");

            //gvAksesuar.FormatAsNumber(1, "Qty", "IDLEQty");
            gvAyrim.FormatAsNumber(0, "QtySelected", "QtyRevised");

            gvIstek.MakeColumnInvisible("IDLE", "isRezerve", "RzvTill", "RzvFor");

            //gvAksesuar.MakeColumnInvisible("AksesuarIstekId");
            //gvAksesuar.Columns["IDLEQty"].VisibleIndex = IsOnay ? gvAksesuar.Columns["Qty"].VisibleIndex + 1 : -1;

            gvAyrim.MakeColumnInvisible("Onay", "AksesuarIstekId", "QtyRevised");

            //gvAksesuar.BestFitColumns();

            gvAyrim.RowStyle += new DevExpress.XtraGrid.Views.Grid.RowStyleEventHandler(gvTop_RowStyle);
            gvIstek.RowCellStyle += new RowCellStyleEventHandler(gvIstek_RowCellStyle);
            //gvAksesuar.RowCellStyle += new RowCellStyleEventHandler(gvIstek_RowCellStyle);
            //gvAksesuar.AddSummary("Qty", "IDLEQty");
            gvIstek.AddSummary();
            gvAyrim.AddSummary("QtySelected");

            gvAyrim.OptionsView.EnableAppearanceEvenRow = gvAyrim.OptionsView.EnableAppearanceOddRow = false;
            //gvAyrim.Appearance.OddRow.BackColor = Color.WhiteSmoke;

            gvIstek.BestFitColumns();
        }


        void gvIstek_RowCellStyle(object sender, DevExpress.XtraGrid.Views.Grid.RowCellStyleEventArgs e)
        {
            if (e.RowHandle > -1)
                if (e.Column.FieldName == "O" ||
                    e.Column.FieldName == "R")
                    e.Appearance.Font = new Font(AppearanceObject.DefaultFont, FontStyle.Bold);

        }

        void gvTop_RowStyle(object sender, DevExpress.XtraGrid.Views.Grid.RowStyleEventArgs e)
        {
            GridView view = sender as GridView;
            if (e.RowHandle > -1)
            {
                object cellValue = view.GetRowCellValue(e.RowHandle, "Onay");
                if (cellValue != null && cellValue != DBNull.Value)
                {
                    if ((bool)cellValue == true)
                        e.Appearance.BackColor = Color.YellowGreen;
                    else
                        e.Appearance.BackColor = Color.OrangeRed;
                }
                else
                    e.Appearance.Combine((e.RowHandle & 1) != 0 ? view.Appearance.EvenRow : view.Appearance.OddRow);
            }
        }

        int AksesuarIstekId = 0;
        void bs1_CurrentItemChanged(object sender, EventArgs e)
        {
            txtInfo.Caption = "İstek: " + bs1.GetCurrentValue("AksesuarIstekId");
        }

        DataRow[] GetTopP(int IstekId, int ProductId)
        {
            return gvAyrim.DataTable().Select(string.Format("AksesuarIstekId= {0} AND ProductId = {1}", IstekId, ProductId));
        }

        DataRow[] GetTopI(int IstekId)
        {
            return gvAyrim.DataTable().Select(string.Format("AksesuarIstekId= {0}", IstekId));
        }


        void IstegiSec(object sender, EventArgs e)
        {
            if (gvIstek.SelectedRowsCount < 1)
                gvIstek.SelectRow(gvIstek.FocusedRowHandle);

            foreach (int handle in gvIstek.GetSelectedRows())
            {
                AksesuarIstekId = gvIstek.GetRowCellValue(handle, "AksesuarIstekId").ToInt(-1);
                foreach (DataRow row in GetTopI(AksesuarIstekId))
                    row["Onay"] = true;
            }
        }

        void IstegiBirak(object sender, EventArgs e)
        {
            if (gvIstek.SelectedRowsCount < 1)
                gvIstek.SelectRow(gvIstek.FocusedRowHandle);

            foreach (int handle in gvIstek.GetSelectedRows())
            {
                AksesuarIstekId = gvIstek.GetRowCellValue(handle, "AksesuarIstekId").ToInt(-1);
                foreach (DataRow row in GetTopI(AksesuarIstekId))
                    row["Onay"] = false;
            }
        }


        void TopuSec(object sender, EventArgs e)
        {
            if (gvAyrim.SelectedRowsCount < 1)
                gvAyrim.SelectRow(gvAyrim.FocusedRowHandle);

            foreach (int handle in gvAyrim.GetSelectedRows())
                gvAyrim.SetRowCellValue(handle, "Onay", true);
        }

        void TopuBirak(object sender, EventArgs e)
        {
            if (gvAyrim.SelectedRowsCount < 1)
                gvAyrim.SelectRow(gvAyrim.FocusedRowHandle);

            foreach (int handle in gvAyrim.GetSelectedRows())
                gvAyrim.SetRowCellValue(handle, "Onay", false);
        }


        private void bbiKaydet_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {

            int ra = gvIstek.Update("", "AksesuarIstek").Rows.Count;
            if (ra > 0)
                Notify(string.Format("{0} satır güncellendi.", ra));

            gvIstek.FormatAsNumber(0, "O", "R", "I");
            gvIstek.AddSummary();

            bs1.EndEdit();
            //bs2.EndEdit();
            bs3.EndEdit();

            DataTable dt = gvAyrim.DataTable().Clone();

            foreach (DataRow dr in gvAyrim.DataTable().Select("Onay is not null"))
                dt.ImportRow(dr);

            if (dt.Rows.Count > 0)
            {
                splitContainerControl.Enabled = false;
                string xml = dt.Serialize();
                DataTable dti = ExecuteDataTable("AksesuarIstekOnayla", xml, WhoAmI);

                if (DevExpress.XtraEditors.XtraMessageBox.Show("Onayladığınız istekler için satınalma talebi oluşturmak ister misiniz ?", "Critical", MessageBoxButtons.YesNo) == System.Windows.Forms.DialogResult.Yes)
                {
                    AksesuarSatinalmaTalebi ast = new AksesuarSatinalmaTalebi(dt);
                    DialogResult dr = ast.ShowDialog();

                }

                foreach (DataRow dr in dti.Rows)
                {
                    AksesuarIstekId = dr["AksesuarIstekId"].ToInt(-1);
                    string to = dr["Email"].ToString();

                    Mail.Send(to, "Aksesuar Ayrım İsteği " + AksesuarIstekId.ToStr(), string.Format("{0} Id'li aksesuar ayrım isteğinize cevap verilmiştir. Lütfen kontrol ediniz.", AksesuarIstekId), "");
                }
                bbiGuncelle.PerformClick();
            }
            splitContainerControl.Enabled = true;
        }

        private void bbiGuncelle_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            gvIstek.RefreshDataSource("GetAksesuarIstek", IsOnay, WhoAmI);
        }
    }
}